﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Talon's Blog </title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="apple-touch-icon" href="icon/apple-touch-icon.png">
    <!--收藏夹显示图标-->
    <link rel="shortcut icon" href="icon/logo.ico" type="image/x-icon">
    <!--网页标题左侧显示-->
    <link rel="icon" href="icon/logo.ico" type="image/x-icon">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css">
    <!-- 引入 echarts.js -->
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.0/echarts.min.js"></script>
</head>

<body>
    <?php include('data.php'); ?>

    <div class="container">
        <!-- <div class="jumbotron">
            <h1>Talon's</h1>
            <p>The only time you should ever look back is to <a
                    href="https://www.iermu.com/video/4b3d46d07007176c17621599cbd3f40e/102051?lang=zh-Hans">see</a> how
                far you've come.</p>
        </div> -->

        <div class="row">
            <div class="col-md-6">
                <h3>最新</h3>
                <style>
                    table {
                        border-collapse: collapse;
                        width: 100%;
                        max-width: 400px;
                        margin: auto;
                    }

                    th,
                    td {
                        padding: 8px;
                        text-align: left;
                        border-bottom: 1px solid #ddd;
                    }

                    th {
                        background-color: #f2f2f2;
                    }
                </style>
                <table>
                    <tr>
                        <td>时间</td>
                        <td>
                            <?php echo $lastTime; ?>
                        </td>
                    </tr>
                    <tr>
                        <td>温度</td>
                        <td>
                            <?php echo end($temp); ?> ℃
                        </td>
                    </tr>
                    <tr>
                        <td>湿度</td>
                        <td>
                            <?php echo end($humi); ?> %
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <h3>统计</h3>
                <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
                <div id="main" style="width: 100%;height:500px;"></div>
                <script type="text/javascript">
                    // 基于准备好的dom，初始化echarts实例
                    var myChart = echarts.init(document.getElementById('main'));
                    // 假设已经将时间、温度、湿度存储在对应的数组中
                    var timeData = <?php echo json_encode($time); ?>;
                    var tempData = <?php echo json_encode($temp); ?>;
                    var humiData = <?php echo json_encode($humi); ?>;

                    // 配置 option
                    var option = {
                        title: {
                            text: '环境信息'
                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data: ['温度', '湿度']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        toolbox: {
                            feature: {
                                saveAsImage: {}
                            }
                        },
                        xAxis: {
                            type: 'time' // 将 X 轴类型设置为时间类型
                        },
                        yAxis: {
                            type: 'value', // 将 Y 轴类型设置为数值类型
                            min: 0 // 设置 Y 轴最小值
                        },
                        series: [{
                            name: '温度', // 设置 series 名称
                            type: 'line',
                            data: [],
                            markPoint: {
                                data: [
                                    { type: 'max', name: '最大值' },
                                    { type: 'min', name: '最小值' }
                                ]
                            }
                        }, {
                            name: '湿度', // 设置 series 名称
                            type: 'line',
                            data: [],
                            markPoint: {
                                data: [
                                    { type: 'max', name: '最大值' },
                                    { type: 'min', name: '最小值' }
                                ]
                            }
                        }]
                    };

                    // 将时间数据转换为时间戳
                    for (var i = 0; i < timeData.length; i++) {
                        timeData[i] = new Date(timeData[i]).getTime();
                    }

                    // 将时间戳和温度、湿度数据合并为一个数组
                    var combinedData = [];
                    for (var i = 0; i < timeData.length; i++) {
                        combinedData.push([timeData[i], tempData[i], humiData[i]]);
                    }

                    // 将合并后的数据分别设置为 series 的 data
                    option.series[0].data = combinedData.map(function (item) {
                        return [item[0], item[1]];
                    });
                    option.series[1].data = combinedData.map(function (item) {
                        return [item[0], item[2]];
                    });

                    // 使用刚刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                </script>
            </div>
        </div>
    </div>

</body>

</html>